<?php
class MarketAction extends LoginAction
{
    protected $type = 'market';
    public function index ()
    {
        $MarketModel = D('Market');
        $market = $MarketModel->getAllData();
        $this->assign('market', $market);
        $this->display();
    }
    /**
     * 添加商场操作
     * @author Ice iceinto@mallog.com.cn
     */
    public function add ()
    {
        $this->getOtherData();
        if (isPost()) {
            $data = $this->getData();
            $status = $this->createAdd('Market', $data);
        }
        $this->display();
    }
    /**
     * 修改商品操作
     * @author Ice iceinto@mallog.com.cn
     */
    public function edit ()
    {
        $id = (int) getGetOrPost('id');
        $map = array();
        $map['m_market.id'] = $id;
        $MarketModel = D('Market');
        $market = $MarketModel->getOnly($map);
        if (! $market) {
            $this->error('数据传输有错误！');
        } else {
            $this->getOtherData();
            if (isPost()) {
                $data = $this->getData(true, $id);
                $data['id'] = $id;
                //$MarketModel->data($data)->save()
                $rs = $this->createUpdate('Market', $data);
                if ($rs) {
                    $this->successMessage('修改成功！');
                    //修改成功删除原有图片
                    if (($market['image'] != $data['image']) && $data['image']) {
                        $this->delUpLoadFile($market['image'], $this->type);
                    }
                    //重新加载信息
                    $market = $MarketModel->getOnly($map);
                } else {
                    $this->errorMessage('修改失败！');
                }
            }
             //加载品牌图片
        }
        $imageUrl = $this->getManagedUrl($market['image'], $this->type);
        $this->assign('imageUrl', $imageUrl);
        $this->assign('market', $market);
        $this->display();
    }
    /**
     * 获取其他参数
     * @author Ice iceinto@mallog.com.cn
     */
    public function getOtherData ()
    {
        $ProvinceModel = M('Province');
        $province = $ProvinceModel->select();
        $BusModel = M('Bus');
        $bus = $BusModel->select();
        $this->assign('bus', $bus);
        $this->assign('province', $province);
    }
    /**
     * 获取提交数据
     * @author Ice iceinto@mallog.com.cn
     */
    public function getData ($isEdit = false, $id = 0)
    {
        $data = array();
        $data['province'] = (int) getPost('province');
        $data['city'] = (int) getPost('city');
        $data['area'] = (int) getPost('area');
        $data['cbd'] = (int) getPost('bus');
        $data['name'] = getPost('mName');
        $data['initial'] = strtolower(getPost('fChar'));
        $data['b_time'] = getPost('bTime');
        $data['intr'] = getPost('marketIntr');
        $data['contact'] = getPost('contact');
        //图片处理
        $doUpload = true;
        if ($isEdit) {
            if (! $_FILES['image']['name']) {
                $doUpload = false;
            }
        }
        if ($doUpload) {
            $fId = $this->uploadFiles($this->type);
            $data['image'] = $fId; //媒体库标示
            if(!$data['image']){
            	$this->error('图片传送失败');
            }
        }
        //判断必要性
        foreach ($data as $k => $d) {
            if (! $d) {
                $this->error($k . '不能为空！');
            }
        }
        return $data;
    }
    /**
    * 获取商场基本信息
    * @param int $id 商场标识
    * @param string $field 获取字段
    * @return array 商场信息
    * @author Ice <iceinto@mallog.com.cn>
    */
    public function getById($id,$field = 'id,name,image,initial'){
    	return $this->field($field)->find($id);
    }
}
?>